package Stack;
import ArrList.ArrList;

public class Stack<T> implements IStack<T> {
    private ArrList element; // 存储栈数据元素的数组
    private int top; // 栈顶元素下标

    public Stack(int size) {
        this.element = new ArrList(Math.abs(size));
        this.top = -1;
    }

    @Override
    public boolean isEmpty() {
        if(this.top == -1){
            return true;
        }
        return false;
    }

    @Override
    public void push(Object x) {
        if(x == null){//判断压入的元素是否为空
            return;
        }
        if (isFull()){//栈满扩容
            ArrList temp = new ArrList(this.element.length());
            this.element = new ArrList(this.element.length() + 1);
            for (int i = 0; i < temp.length(); i++) {
                element.set( i, temp.get(i));
            }
        }
        this.top++;
        this.element.set( this.top, x);
    }

    @Override
    public boolean isFull() {
        if (this.top == this.element.length() - 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public T popTop() {
        if (isEmpty()){
            return null;
        }else {
            this.top--;
            return (T) this.element.get(this.top + 1);
        }
    }

    @Override
    public T getTop() {
        return (T) this.element.get(this.top);
    }
}
